package com.tjise.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjise.eduservice.client.VodClient;
import com.tjise.eduservice.entity.EduVideo;
import com.tjise.eduservice.mapper.EduVideoMapper;
import com.tjise.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author TJISE
 * @since 2021-06-23
 */
@Service
@Slf4j
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    // 注入vodclient
    @Autowired
    private VodClient vodClient;

    /**
     * 根据课程的id删除小节
     *
     * @param courseId 课程的id
     */

    @Override
    public void removeVideoByCourseId(String courseId) {

        // 根据课程的id删除 所以需要查询条件
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        wrapperVideo.select("video_source_id");
        // 根据课程id查询出视频id查询出来
        List<EduVideo> eduVideos = baseMapper.selectList(wrapperVideo);
        List<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            // 判断如果有值才进行删除 如果没有就不进行删除
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)) {
                videoIds.add(videoSourceId);
            }


        }
        log.info("-------------EduVideoServiceImpl类中的removeVideoByCourseId()方法 eduVideos id 值为：----------------------------------------" + videoIds);
        // 根据多个视频id 删除视频
        if (videoIds.size() > 0) {
            // 判断集合中是否有值 如果有就进行删除
            vodClient.deleteBatch(videoIds);
        }

        int delete = baseMapper.delete(wrapper);
        log.info("---------------EduVideoServiceImpl类中的removeVideoByCourseId()方法删除的为：-------------------------------" + delete);


    }
}
